package org.xtext.ic.dsl.generator;

import com.google.common.base.Objects;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.xtext.ic.dsl.IcDslExtensions;
import org.xtext.ic.dsl.icDsl.Connection;
import org.xtext.ic.dsl.icDsl.CreateDAO;
import org.xtext.ic.dsl.icDsl.DataBase;
import org.xtext.ic.dsl.icDsl.Entity;
import org.xtext.ic.dsl.icDsl.Layer;
import org.xtext.ic.dsl.icDsl.Model;
import org.xtext.ic.dsl.icDsl.Type;

@SuppressWarnings("all")
public class HibernateAnnotationsSessionExtensions extends IcDslExtensions {
  public CharSequence interfaceSessao(final Model m, final Connection c) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _packageHeader = this.packageHeader(m, c);
    _builder.append(_packageHeader, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("public interface Sessao {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("final String DATABASE = \"");
    String _dbname = c.getDbname();
    _builder.append(_dbname, "	");
    _builder.append("\";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    {
      DataBase _type = c.getType();
      boolean _isMysql = _type.isMysql();
      if (_isMysql) {
        _builder.append("\t");
        _builder.append("final String DRIVER = \"org.gjt.mm.mysql.Driver\";");
        _builder.newLine();
        _builder.newLine();
        _builder.append("\t");
        _builder.append("final String DIALECT = \"org.hibernate.dialect.MySQLDialect\";");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        {
          boolean _and = false;
          String _url = c.getUrl();
          boolean _equals = _url.equals(null);
          if (!_equals) {
            _and = false;
          } else {
            String _port = c.getPort();
            boolean _equals_1 = _port.equals(null);
            _and = (_equals && _equals_1);
          }
          if (_and) {
            _builder.append("\t");
            _builder.append("final String URL = \"jdbc:mysql://localhost:3306/\" + DATABASE;");
            _builder.newLine();
          } else {
            String _url_1 = c.getUrl();
            boolean _equals_2 = _url_1.equals(null);
            boolean _not = (!_equals_2);
            if (_not) {
              _builder.append("\t");
              _builder.append("final String URL = \"jdbc:mysql://");
              String _url_2 = c.getUrl();
              _builder.append(_url_2, "	");
              _builder.append(":3306/\" + DATABASE;");
              _builder.newLineIfNotEmpty();
            } else {
              String _port_1 = c.getPort();
              boolean _equals_3 = _port_1.equals(null);
              boolean _not_1 = (!_equals_3);
              if (_not_1) {
                _builder.append("\t");
                _builder.append("final String URL = \"jdbc:mysql://localhost:");
                String _port_2 = c.getPort();
                _builder.append(_port_2, "	");
                _builder.append("/\" + DATABASE;");
                _builder.newLineIfNotEmpty();
              } else {
                _builder.append("\t");
                _builder.append("final String URL = \"jdbc:mysql://");
                String _url_3 = c.getUrl();
                _builder.append(_url_3, "	");
                _builder.append(":");
                String _port_3 = c.getPort();
                _builder.append(_port_3, "	");
                _builder.append("/\" + DATABASE;");
                _builder.newLineIfNotEmpty();
              }
            }
          }
        }
      } else {
        DataBase _type_1 = c.getType();
        boolean _isSqlserver = _type_1.isSqlserver();
        if (_isSqlserver) {
        } else {
          DataBase _type_2 = c.getType();
          boolean _isOracle = _type_2.isOracle();
          if (_isOracle) {
          }
        }
      }
    }
    _builder.newLine();
    {
      String _user = c.getUser();
      boolean _equals_4 = _user.equals(null);
      if (_equals_4) {
        _builder.append("\t");
        _builder.append("final String USER = \"root\";");
        _builder.newLine();
      } else {
        _builder.append("\t");
        _builder.append("final String USER = \"");
        String _user_1 = c.getUser();
        _builder.append(_user_1, "	");
        _builder.append("\";");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.newLine();
    {
      String _password = c.getPassword();
      boolean _equals_5 = _password.equals(null);
      if (_equals_5) {
        _builder.append("\t");
        _builder.append("final String PASSWORD = \"root\";");
        _builder.newLine();
      } else {
        _builder.append("\t");
        _builder.append("final String PASSWORD = \"");
        String _password_1 = c.getPassword();
        _builder.append(_password_1, "	");
        _builder.append("\";");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.newLine();
    {
      String _poolsize = c.getPoolsize();
      boolean _equals_6 = _poolsize.equals(null);
      if (_equals_6) {
        _builder.append("\t");
        _builder.append("final String POOL_SIZE = \"10\";");
        _builder.newLine();
      } else {
        _builder.append("\t");
        _builder.append("final String POOL_SIZE = \"");
        String _poolsize_1 = c.getPoolsize();
        _builder.append(_poolsize_1, "	");
        _builder.append("\";");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence fabricaSessao(final Model m, final Connection c, final CreateDAO cdao) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _packageHeader = this.packageHeader(m, c);
    _builder.append(_packageHeader, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    {
      Entity _firstEntity = cdao.getFirstEntity();
      boolean _notEquals = (!Objects.equal(_firstEntity, null));
      if (_notEquals) {
        Entity _firstEntity_1 = cdao.getFirstEntity();
        CharSequence _importEntity = this.importEntity(m, _firstEntity_1);
        _builder.append(_importEntity, "");
        _builder.newLineIfNotEmpty();
        {
          EList<Entity> _entities = cdao.getEntities();
          for(final Entity e : _entities) {
            CharSequence _importEntity_1 = this.importEntity(m, e);
            _builder.append(_importEntity_1, "");
            _builder.newLineIfNotEmpty();
          }
        }
      }
    }
    _builder.append("import org.hibernate.HibernateException;");
    _builder.newLine();
    _builder.append("import org.hibernate.Session;");
    _builder.newLine();
    _builder.append("import org.hibernate.SessionFactory;");
    _builder.newLine();
    _builder.append("import org.hibernate.cfg.Configuration;");
    _builder.newLine();
    _builder.append("import org.hibernate.service.ServiceRegistry;");
    _builder.newLine();
    _builder.append("import org.hibernate.service.ServiceRegistryBuilder;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("public class FabricaSessao implements Sessao {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("private FabricaSessao() {");
    _builder.newLine();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("private static SessionFactory sf;");
    _builder.newLine();
    _builder.append("    ");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public static void criaFabricaSessao(){");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("if(sf == null){");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("Configuration con = new Configuration();");
    _builder.newLine();
    _builder.append("                ");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("con.setProperty(\"hibernate.connection.driver_class\", DRIVER);");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("con.setProperty(\"hibernate.connection.url\", URL);");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("con.setProperty(\"hibernate.connection.username\", USER);");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("con.setProperty(\"hibernate.connection.password\", PASSWORD);");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("con.setProperty(\"hibernate.connection.pool_size\", POOL_SIZE);");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("con.setProperty(\"hibernate.dialect\", DIALECT);");
    _builder.newLine();
    _builder.append("                ");
    _builder.newLine();
    {
      Entity _firstEntity_2 = cdao.getFirstEntity();
      boolean _notEquals_1 = (!Objects.equal(_firstEntity_2, null));
      if (_notEquals_1) {
        _builder.append("                ");
        _builder.append("con.addAnnotatedClass(");
        Entity _firstEntity_3 = cdao.getFirstEntity();
        String _name = _firstEntity_3.getName();
        _builder.append(_name, "                ");
        _builder.append(".class);");
        _builder.newLineIfNotEmpty();
        {
          EList<Entity> _entities_1 = cdao.getEntities();
          for(final Entity e_1 : _entities_1) {
            _builder.append("                ");
            _builder.append("con.addAnnotatedClass(");
            String _name_1 = e_1.getName();
            _builder.append(_name_1, "                ");
            _builder.append(".class);");
            _builder.newLineIfNotEmpty();
          }
        }
      }
    }
    _builder.append("                ");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("ServiceRegistry sr = new ServiceRegistryBuilder()");
    _builder.newLine();
    _builder.append("                    ");
    _builder.append(".applySettings(con.getProperties()).buildServiceRegistry();  ");
    _builder.newLine();
    _builder.append("                ");
    _builder.newLine();
    _builder.append("                ");
    _builder.append("sf = con.buildSessionFactory(sr);");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("    ");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public static Session getSessao() {");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("Session session = null;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("criaFabricaSessao();");
    _builder.newLine();
    _builder.append("            ");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("session = sf.openSession();");
    _builder.newLine();
    _builder.append("            ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("} catch (HibernateException ex) {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("System.out.println(\"Problemas ao carregar a Sess\u00E3o!\");");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("System.out.println(\"ERRO: \" + ex.getMessage());");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("return session;");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("    ");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public static void fechar(Session session) {");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("try { ");
    _builder.newLine();
    _builder.append("           ");
    _builder.append("session.disconnect();");
    _builder.newLine();
    _builder.append("           ");
    _builder.append("session.close();");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("} catch (HibernateException ex) {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("System.out.println(\"Problemas ao fechar: \" + ex.getMessage());");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence annotatedClass(final Layer l) {
    StringConcatenation _builder = new StringConcatenation();
    {
      EList<Type> _types = l.getTypes();
      for(final Type e : _types) {
        {
          if ((e instanceof Entity)) {
            _builder.append("con.addAnnotatedClass(");
            String _name = ((Entity) e).getName();
            _builder.append(_name, "");
            _builder.append(".class);");
            _builder.newLineIfNotEmpty();
          } else {
            CharSequence _annotatedClass = this.annotatedClass(((Layer) e));
            _builder.append(_annotatedClass, "");
            _builder.newLineIfNotEmpty();
          }
        }
      }
    }
    return _builder;
  }
}
